<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <!--如果是IE 就以标准渲染-->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <!-- 视窗——————响应式布局 -->
  <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
  <!--当前页面的三要素-->
  <title>运维系统-登录</title>
  <meta name="description" content="XXXXX" />
  <meta http-equiv="keywords" content="XXXXX" />
  <!-- js公共函数 -->
  <!-- <script type="text/javascript" src="./js/common/commonfuntion.js"></script> -->
  <!-- css -->
  <link rel="stylesheet" type="text/css" href="./css/logoin.css" />
</head>

<body>
  <div id="container" class="logo">
    <div class="logo_01">
      <!--主屏内容-->
      <form class="logo_screen">
        <div class="tit_first"> Hi ,<br /> Welcome! </div>
        <div class="tit_second"> XXXXX远程运维管理系统 </div>
        <div class="dl"> 登 录 </div>
        <div class="inputdiv">
          <!-- <input type="text" placeholder="输入账号" lay-verify="required|userName" /> -->
          <input type="text" placeholder="输入账号" id="username" />
          <!-- <input type="text" placeholder="输入密码" lay-verify="required|password" /> -->
          <input type="password" placeholder="输入密码" id="password" />
        </div>
        <div class="remindme clear">
          <div class="zdtl">
            <input class="zdinput" type="checkbox" id="autologoin" checked='true' />记住我
          </div>
        </div>
        <button id="regBtn" class="btnclass"> 登 录 </button>
        <a href="#" style="font-size:0.24rem;display:block;width:100%;text-align:center;margin-top:0.5rem;"> 《隐私权政策声明》
        </a>
      </form>
    </div>
  </div>
  <script>
    (function (win, doc) {
      var docEl = doc.documentElement || document.body; //获取HTML标签
      var container = doc.getElementById("container"); //container元素
      //判断是移动端设备还是PC,移动 就采用'orientationchange',横竖屏事件，PC端就采用onresize，窗口改变时间
      var resize = "onorientationchange" in win ? "orientationchange" : "resize";

      function rem() {
        docEl.style.fontSize = 100 * (container.clientWidth / 750) + "px";
      }
      //监听'DOMContent事件:DOM加载完成执行,如果DOMContent事件，那么执行rem函数
      doc.addEventListener("DOMContentLoaded", rem, false);
      //win下监听resize事件,如果resize事件，那么执行rem函数
      win.addEventListener(resize, rem, false);
    })(window, document);
  </script>
  <!-- js -->
  <script type="text/javascript" src="./js/jquery-3.4.1.js"></script>
  <script type="text/javascript" src="./layer/layer.js"></script>
  <script>
    $(document).ready(function () {
      window.localStorage.setItem("accessToken", "");
      window.localStorage.setItem("useraccount", "");
      window.localStorage.setItem("password", "");
      window.localStorage.setItem("name", "");
      window.localStorage.setItem("userName", "");
      window.localStorage.setItem("id", "");
      window.localStorage.setItem("surname", "");
      // 进入页面后显示账号和密码
      if (window.localStorage.getItem("useraccount")) {
        if ($("#autologoin").prop("checked")) {
          $("#username").attr("value", window.localStorage.getItem("useraccount"));
          $("#password").attr("value", window.localStorage.getItem("password"));
        } else {
          $("#username").attr("value", '');
          $("#password").attr("value", '');
        }
      } else {
        $("#username").attr("value", 'admin');
        $("#password").attr("value", 'Jnyd080808');
      }
      // 复选框选中事件
      $('#autologoin').change(function () {
        if ($("#autologoin").prop("checked")) {
          $("#username").attr("value", ' ');
          $("#password").attr("value", ' ');
          $("#username").attr("value", window.localStorage.getItem("useraccount"));
          $("#password").attr("value", window.localStorage.getItem("password"));
        } else {
          $("#username").attr("value", ' ');
          $("#password").attr("value", ' ');
        }
      });
      // 点击登录按钮
      $("#regBtn").click(function () {
        $("#regBtn").html(' ');
        $("#regBtn").html('登录中...');
        $("#regBtn").attr("disabled", true);
        $("#regBtn").attr('class', 'btnclass_dis');
        var username = $("#username").val();
        var password = $("#password").val();
        if (!(username != "" && password != "")) {
          layer.msg("请输入账号和密码", {
            icon: 5,
            time: 1000 //1秒关闭（如果不配置，默认是3秒）
          }, function () {
            $("#regBtn").html('');
            $("#regBtn").html('登 录');
            $("#regBtn").attr("disabled", false);
            $("#regBtn").attr('class', 'btnclass');
          });
        } else {
          var params = {
            userNameOrEmailAddress: username,
            password: password,
            rememberClient: false
          };
          params = JSON.stringify(params);
          $.ajax({
            url: "http://112.74.169.99:21021/api/TokenAuth/Authenticate",
            data: params,
            type: "post",
            dataType: "json",
            beforeSend: function (xhr) {
              xhr.setRequestHeader("Content-Type", "application/json");
              xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
            },
            success: function (res) {
              // console.log("accessToken 请求成功");
              if (res.success === true) {
                if (!window.localStorage.accessToken) {
                  window.localStorage.setItem("accessToken", res.result.accessToken);
                  window.localStorage.setItem("useraccount", username);
                  window.localStorage.setItem("password", password);
                }
                var accessToken = "Bearer " + window.localStorage.getItem("accessToken");
                $.ajax({
                  type: "GET",
                  url: "http://112.74.169.99:21021/api/services/app/Session/GetCurrentLoginInformations",
                  beforeSend: function (xhr) {
                    xhr.setRequestHeader("Accept",
                    "application/json, text/javascript, */*; q=0.01");
                    xhr.setRequestHeader("Authorization", accessToken);
                    xhr.setRequestHeader(".AspNetCore.Culture", "zh-Hans");
                  },
                  success: function (res) {
                    // console.log(res);
                    // return;
                    if (!window.localStorage.name) {
                      window.localStorage.setItem("name", res.result.user.name);
                      window.localStorage.setItem("userName", res.result.user.userName);
                      window.localStorage.setItem("id", res.result.user.id);
                      window.localStorage.setItem("surname", res.result.user.surname);
                    }
                    layer.msg('登录成功', {
                      icon: 1,
                      time: 1000 //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                      window.location.href = 'index.html';
                    });
                  },
                  error: function (err) {
                    layer.msg('登录失败', {
                      icon: 1,
                      time: 1000 //1秒关闭（如果不配置，默认是3秒）
                    }, function () {
                      $("#regBtn").html('');
                      $("#regBtn").html('登 录');
                      $("#regBtn").attr("disabled", false);
                      $("#regBtn").attr('class', 'btnclass');
                    });
                    console.log("用户数据 请求失败");
                  }
                });
              }
            },
            error: function (err) {
              layer.msg('登录失败 请输入正确的账号密码', {
                icon: 2,
                time: 1000
              }, function () {
                $("#regBtn").html('');
                $("#regBtn").html('登 录');
                $("#regBtn").attr("disabled", false);
                $("#regBtn").attr('class', 'btnclass');
              });
              console.log("accessToken 请求失败");
            }
          });
        }
      });
    });
  </script>
</body>

</html>